home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / misc / fpl-v13.lha / fpl / util / FrexxCalc.c < prev    next >
C/C++ Source or Header  |  1994-06-02  |  6KB  |  192 lines

  1. ;/* Execute me to compile with Lattice 5.10 or SAS/C 6.0
  2. if exists SC:
  3.    sc DEFINE=AMIGA STRINGMERGE NOSTKCHK LINK MODIFIED DATA=NEAR IGNORE=73 OPTIMIZE FrexxCalc.c
  4. else if exists LC:
  5.    lc -DAMIGA -b1 -Lcd -v -M -j73 -O FrexxCalc.c
  6. else
  7.    echo "Didn't find SAS/C version 5 or 6. Compile by yourself!"
  8. endif
  9. quit
  10.  
  11. *---------------------------------------------------------------------------*
  12. UNIX:
  13. Compile string "cc -O -DUNIX -o FrexxCalc FrexxCalc.c -lfpl -L<fpllib dir>"
  14. *---------------------------------------------------------------------------*
  15.  
  16. *****************************************************************************
  17. * FrexxCalc.c - Calculates a C-style expression.                            *
  18. *                                                                           *
  19. * Copyright (C) 1992, 1993 by FrexxWare.                                    *
  20. * Author: Daniel Stenberg                                                   *
  21. *                                                                           *
  22. * This example is provided "as-is" and is subject to change; no warranties  *
  23. * are made.  All use is at your own risk.  No liability or responsibility   *
  24. * is assumed.                                                               *
  25. *                                                                           *
  26. ****************************************************************************/
  27.  
  28. #ifdef AMIGA
  29. #include <exec/types.h>
  30. #include <exec/libraries.h>
  31. #include <libraries/dos.h>
  32.  
  33. #include <proto/exec.h>
  34. #include <proto/FPL.h>
  35. #include <stdlib.h>
  36.  
  37. #include <libraries/FPL.h>
  38. struct Library *FPLBase = NULL; /* library base */
  39.  
  40. #define PREFIX __asm
  41. #define REG(x) register __ ## x
  42. #elif UNIX /* AMIGA */
  43. #include <sys/types.h>
  44. #include "../src/FPL.h"
  45. #define PREFIX /* ignored */
  46. #define REG(x)
  47. #endif
  48.  
  49. #include <stdio.h>
  50. #include <string.h>
  51.  
  52. long PREFIX functions(REG(a0) struct fplArgument *);
  53.  
  54. #define OUTPUT_FUNCTION 2
  55.  
  56. #define FPLNAME "fpl.library"
  57.  
  58. #ifdef AMIGA
  59. static char *version="\0$VER: FrexxCalc 5.1 (15.4.94)"; /* version */
  60.      /*
  61.       * From version 5.1, FrexxCalc can be compiled to be pure.
  62.       */
  63. #endif
  64.  
  65. int main(int argc, char **argv)
  66. {
  67.   int n, end, len;    /* some all purpose variables */
  68.   void *anchor;        /* FPL use pointer */
  69.   char *program;
  70.  
  71.   /*
  72.    * As you can see in the FPL program, we're taking advantage of the
  73.    * internal function eval() which returns the result of the expression
  74.    * held by a string. (Ex: if the string variable Expr contains the string
  75.    * "2+2", eval(Expr) will return 4.)
  76.    *
  77.    * We create a string to hold the program:
  78.    * "Output(eval( command_line_arguments ));" which is interpreted by
  79.    * FPL.
  80.    */
  81.  
  82.   /*
  83.    * First we check the number of arguments, and if no expression
  84.    * is given, output a usage text.
  85.    */
  86.  
  87.   if(argc < 2) {    /* if there was no expression given */
  88.     printf("Usage: FrexxCalc [expression]\n\n"
  89.        "* ALL C operators are supported. E.g:\n"
  90.        "  -, /, *, %, +, ==, <=, >=, >, <, !, ~, (), &&, ||, ^, &, >>, <<, |, ? and :.\n\n"
  91.        "* Functions supported are:\n"
  92.        "  abs, atoi, eval, strcmp, substr, strlen, strncmp, strstr, strtol, ltostr\n\n"
  93.        "* Hexadecimal numbers are written with a 0x prefix, octal numbers with\n"
  94.        "  a 0 prefix and binary numbers with a 0b prefix.\n\n"
  95.        "* Strings (inclosed in quotation marks) and characters (inclosed in\n"
  96.        "  apostropes) supports ALL C language  escape sequences\n\n"
  97.        "* Expression example:\n"
  98.        "  abs(3*(strcmp(\"hi\",\"bye\")-\'a\')*0xcf-0b10010*030)<<2\n\n"
  99.        "* Read FPL.guide for closer documentation about the expressions.\n\n"
  100.        "Written by Daniel Stenberg 940415, Copyright (C) 1992-1994 by FrexxWare.\n");
  101.     return(0);
  102.   }
  103.  
  104. #ifdef AMIGA
  105.   /*
  106.    * Open fpl.library on amiga.
  107.    */
  108.  
  109.   if(!(FPLBase=OpenLibrary(FPLNAME, 5))) { /* open FPL.library version 5 */
  110.     printf("Error opening %s!\n", FPLNAME);
  111.     return(-1);
  112.   }
  113. #endif
  114.  
  115.   for(len=0, n=1; n<argc; n++) /* count length of all arguments */
  116.     len+=strlen(argv[n]);
  117.  
  118.   program=(char *)malloc(len+40); /* allocate space to build the
  119.                      program in */
  120.   /*
  121.    * Initialize the FPL session!
  122.    */
  123.  
  124.   anchor=fplInit(functions,    /* function handler */
  125.          NULL);        /* tag list */
  126.  
  127.   if(anchor) {
  128.     /*
  129.      * Add the function we want FPL to accept!
  130.      */
  131.     fplAddFunction(anchor,
  132.            "Output",    /* this function will output the result */
  133.            OUTPUT_FUNCTION,/* ID */
  134.            FPL_INTARG,    /* return an integer */
  135.            "I",        /* optional parameter as argument! */
  136.            NULL);    /* no additional data */
  137.     
  138.     /*
  139.      * Get all the members in the argv[] array and concatenate them all into
  140.      * one single string.
  141.      */
  142.     program[0]=0;
  143.     
  144.     strcat(program, "Output(eval(\""); /* FPL program start! */
  145.     for(n=1; n<argc; n++)
  146.       strcat(program, argv[n]);
  147.     strcat(program, "\"));");
  148.  
  149.     /*
  150.      * Initialize the fplData structure.
  151.      */
  152.     
  153.     end=fplExecuteScript(anchor, &program, 1, NULL);/* execute the program */
  154.     free(program); /* free the program */
  155.     fplFree(anchor);
  156.   }
  157. #ifdef AMIGA
  158.   /* If amiga: close the library */
  159.   CloseLibrary((struct Library *)FPLBase);
  160. #endif
  161.   return(0);
  162. }
  163.  
  164. /*
  165.  * The function below is the one that is going to be called each time
  166.  * our previously defined function is discovered in the FPL program.
  167.  * We check the ID member of the fplArgument structure we get access to,
  168.  * to check which of the functions that was invoked.
  169.  * We could also check the name member of the structure since that will
  170.  * point to the function name, but integer comparisons are much faster!
  171.  * The ID will be set to FPL_GENERAL_ERROR if any error was discovered
  172.  * in the FPL program!
  173.  */
  174.  
  175. long PREFIX functions(REG(a0) struct fplArgument *arg)
  176. {
  177.   char buffer[FPL_ERRORMSG_LENGTH];
  178.   switch(arg->ID) {
  179.   case OUTPUT_FUNCTION:    /* if the Output() function was invoked */
  180.     if(arg->format[0] == FPL_INTARG)
  181.       printf("%d\n", (int)arg->argv[0]);
  182.     else
  183.       printf("%s\n", (char *)arg->argv[0]);
  184.     /* returns nothing! */
  185.     break;
  186.   case FPL_GENERAL_ERROR:
  187.     printf("%s\n", fplGetErrorMsg(arg->key, (long)arg->argv[0], buffer));
  188.     break;
  189.   }
  190.   return(0);
  191. }
  192.